package com.erlang.spirngbootmybatis.config;

import com.google.common.collect.Sets;
import io.swagger.annotations.Api;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * 整合swagger2.
 *
 * @author ZGQ
 */
@EnableSwagger2
@Configuration
public class Swagger2DevConfig {

    //默认pro环境
    @Profile({"default", "pro"})
    @Bean
    public Docket createWebApi() {
        return new Docket(DocumentationType.SWAGGER_2).enable(false).select().build();
    }

    //dev环境接口访问地址:http://localhost:8086/swagger-ui.html
    @Profile("dev")
    @Bean
    public Docket createWebApiDev() {
        return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfoDev()).select()
                .apis(RequestHandlerSelectors.withClassAnnotation(Api.class)).paths(PathSelectors.any()).build();
    }

    //测试环境接口访问地址: test.erlang.com/user/swagger-ui.html
    @Profile("test")
    @Bean
    public Docket createWebApiTest() {
        return new Docket(DocumentationType.SWAGGER_2)
                .host("test.erlang.com/user")
                .protocols(Sets.newHashSet("https"))
                .apiInfo(apiInfoDev())
                .select()
                .apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
                .paths(PathSelectors.any()).build();
    }

    private ApiInfo apiInfoDev() {
        return new ApiInfoBuilder()
                // 标题
                .title("用户模块API")
                // 描述
                .description("用户api接口文档\n" + "\n" + "测试环境:https://test.erlang.com/user\n")
                // 作者信息
                .contact(new Contact("Erlang", "https://www.cnblogs.com/erlangha/", "wy259051@163.com"))
                // 版本
                .version("1.0").build();
    }

}
